package com.fencing_squad.homo.list;

import com.bumptech.glide.Glide;
import com.fencing_squad.homo.ResourceTable;
import com.fencing_squad.homo.util.ImageLoader;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class TipProvider extends BaseItemProvider {
    private List<Tip> list;
    private Context context;

    public TipProvider(Context context) {
        list = new ArrayList<>();
        this.context = context;
    }

    public class Holder {
        private Text name;
        private Text address;

        public Holder(Component component) {
            name = (Text) component.findComponentById(ResourceTable.Id_name);
            address = (Text) component.findComponentById(ResourceTable.Id_address);
        }
    }

    public void append(Tip tip) {
        if(list != null) {
            list.add(tip);
            notifyDataChanged();
        }
    }

    public void append(List<Tip> tips) {
        if(list != null) {
            list.addAll(tips);
            notifyDataChanged();
        }
    }

    public void clearAll() {
        if(list != null) {
            list.clear();
            notifyDataChanged();
        }
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        if (list != null && i >= 0 && i < list.size()) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        Tip item = list.get(i);
        Holder holder;
        if (component == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_input_tip, null, false);
            holder = new Holder(cpt);
            cpt.setTag(holder);
        } else {
            cpt = component;
            holder = (Holder)cpt.getTag();
        }
        holder.address.setText(item.getAddress());
        holder.name.setText(item.getName());
        return cpt;
    }
}
